package com.demo.modules.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.common.core.domain.SysUserWxBo;
import com.demo.common.core.exception.ServiceException;
import com.demo.common.core.utils.StringUtils;
import com.demo.common.core.utils.bean.BeanUtils;
import com.demo.common.core.utils.uuid.IdUtils;
import com.demo.common.core.domain.SysUserBo;
import com.demo.common.security.utils.SecurityUtils;
import com.demo.modules.system.common.utils.RandomNameUtil;
import com.demo.modules.system.domain.sys.user.SysUser;
import com.demo.modules.system.domain.sys.user.expand.SysUserExpand;
import com.demo.modules.system.domain.sys.user.wx.SysUserWx;
import com.demo.modules.system.mapper.SysUserExpandMapper;
import com.demo.modules.system.mapper.SysUserMapper;
import com.demo.modules.system.mapper.SysUserWxMapper;
import com.demo.modules.system.service.ISysUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author MyPC
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private SysUserExpandMapper sysUserExpandMapper;

    @Resource
    private SysUserWxMapper sysUserWxMapper;

    @Override
    public SysUserBo findUserByUk(String uk) {
        return sysUserMapper.findUserByUk(uk);
    }

    @Override
    public SysUserBo findUserByMobile(String mobile, Boolean isCreate) {
        return sysUserMapper.findUserByMobile(mobile);
    }

    @Override
    public SysUserBo findUserByMailbox(String mailbox, Boolean isCreate) {
        return sysUserMapper.findUserByMailbox(mailbox);
    }

    @Override
    public SysUserBo findUserByWx(SysUserWxBo sysUserWxBo) {
        SysUserBo sysUserBo = sysUserMapper.findUserByWx(sysUserWxBo.getUnionId(), sysUserWxBo.getOpenId());
        if (StringUtils.isNotNull(sysUserBo)) {
            return sysUserBo;
        }
        registerWx(sysUserWxBo);
        return sysUserMapper.findUserByWx(sysUserWxBo.getUnionId(), sysUserWxBo.getOpenId());
    }


    @Override
    public Boolean registerUser(String uk, String pk) {
        SysUser sysUser = sysUserMapper.selectOne(Wrappers.lambdaQuery(SysUser.class)
                .eq(SysUser::getUk, uk)
                .last("limit 1"));
        if (StringUtils.isNotNull(sysUser)) {
            throw new ServiceException("用户名已存在");
        }
        sysUser = new SysUser();
        sysUser.setUserId(IdUtils.getUuid());
        sysUser.setUk(uk);
        sysUser.setNickName(RandomNameUtil.getName());
        sysUser.setPk(SecurityUtils.encryptPassword(pk));
        return sysUserMapper.insert(sysUser) > 0;
    }

    @Override
    public SysUserBo selectByIdBo(Long userId) {
        return sysUserMapper.selectByIdBo(userId);
    }

    @Transactional(rollbackFor = Exception.class)
    public void registerWx(SysUserWxBo sysUserWxBo) {
        Long userId = IdUtils.getUuid();
        SysUserWx sysUserWx = BeanUtils.copyDataProp(sysUserWxBo, new SysUserWx());
        sysUserWx.setUserId(userId);
        sysUserWxMapper.insert(sysUserWx);
        SysUser sysUser = new SysUser();
        sysUser.setUserId(userId);
        sysUser.setNickName(sysUserWxBo.getNickName());
        sysUser.setAvatar(sysUserWxBo.getHeadImgUrl());
        sysUser.setSex(String.valueOf(sysUserWxBo.getSex()));
        sysUser.setRegisterType("3");
        sysUser.setCreateBy(String.valueOf(userId));
        sysUser.setCreateTime(new Date());
        if (sysUserMapper.insert(sysUser) > 0) {
            SysUserExpand sysUserExpand = new SysUserExpand();
            sysUserExpand.setUserId(userId);
            sysUserExpand.setWechatLogin("Y");
            sysUserExpandMapper.insert(sysUserExpand);
        } else {
            throw new ServiceException("微信注册失败！");
        }
    }


}
